package com.example.dao;

import com.example.domain.Member;
import com.example.domain.Permission;
import com.example.domain.Product;
import com.example.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Date: 2021年08月21日 12:10
 * @Author: LHSAMA
 */
public interface IRoleDao {

    @Select("select * from role where id in (select roleId from users_role where userId = #{userId})")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id",many = @Many(select = "com.example.dao.IPermissionDao.findByRoleId"))
    })
            List<Role> findRoleByUserId(String userId) throws Exception;

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role(roleName,roleDesc) values (#{roleName},#{roleDesc})")
    void save(Role role);

    @Select("select * from role where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id",many = @Many(select = "com.example.dao.IPermissionDao.findByRoleId"))
    })
    Role findById(String id);

    @Delete("delete from role where id = #{id}")
    void deleteRole(String id);

    @Delete("delete from role_permission where roleId = #{id}")
    void deleteRolePermission(String id);

    @Delete("delete from users_role where roleId = #{id}")
    void deleteUsersRole(String id);

    @Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{id})")
    List<Permission> findOtherPermissions(String id);

    @Insert("insert into role_permission values(#{permissionId},#{roleId})")
    void savePermissionToRole(@Param("roleId") String roleId,@Param("permissionId") String permissionId);
}
